package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.model.User;
import com.example.forum.services.IArticleReplyService;
import com.example.forum.services.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: wenjingyuan
 * @Date: 2023/09/28/10:00
 * @Description:
 */
@Slf4j
@RestController
@RequestMapping("reply")
@Api(tags = "回复接口")
public class ArticleReplyController {
    @Resource
    private IArticleReplyService articleReplyService;

    @Resource
    private IArticleService articleService;

    /**
     * 回复帖子
     * @param request
     * @param content
     * @param articleId
     * @return
     */
    @ApiOperation("回复帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("文章内容") @RequestParam("content") @NonNull String content,
                            @ApiParam("文章id") @RequestParam("articleId") @NonNull Long articleId){

        //查看用户是否处于禁言状态
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1){
            //用户已经禁言
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }

        //调用service
        Article article = articleService.selectById(articleId);
        if(article==null||article.getDeleteState()==1){
            //返回错误信息
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //判断帖子状态是否是结束状态
        if(article.getState()==1 ){
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }
        //写入回复信息
        ArticleReply articleReply=new ArticleReply();
        articleReply.setContent(content);
        articleReply.setArticleId(articleId);
        articleReply.setPostUserId(user.getId());
        articleReplyService.create(articleReply);
        log.info("回复成功 article id:"+articleReply.getArticleId());
        return AppResult.success();
    }
    @ApiOperation("获取回复列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@ApiParam("帖子id") @RequestParam("articleId") @NonNull Long articleId){
        Article article = articleService.selectById(articleId);
        //校验帖子是否有效
        if(article==null || article.getDeleteState()==1){
            //返回错误信息
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);
        return AppResult.success(articleReplies);
    }
}
